package net.goutalk.glcs.module.oss.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.goutalk.glcs.common.annotation.GlcsLog;
import net.goutalk.glcs.common.constant.GlobalConstant;
import net.goutalk.glcs.common.exception.MyException;
import net.goutalk.glcs.common.model.result.R;
import net.goutalk.glcs.module.oss.factory.OssFactory;
import net.goutalk.glcs.module.system.entity.File;
import net.goutalk.glcs.module.system.service.IFileService;
import net.goutalk.glcs.module.system.vo.FileVo;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author: tanyujie
 * @Date: 2022/3/8 11:07
 */
@RestController
@RequestMapping(GlobalConstant.SYSTEM_MODULE_PREFIX + "/oss")
@Api(value = GlobalConstant.SYSTEM_MODULE_PREFIX + "/oss", tags = "对象存储模块")
@AllArgsConstructor
public class OssController {


    private final IFileService fileService;

    /**
     * 上传文件
     */
    @PostMapping("/upload")
    @GlcsLog(value = "上传文件")
    @SneakyThrows
    public R upload(@RequestParam("file") MultipartFile file, @RequestParam(value = "folderId",required = false) Long folderId,
                    @RequestParam(value = "processId",required = false) String processId) {
        if (ObjectUtil.isNull(file)) {
            throw new MyException("上传文件不能为空");
        }

        //上传文件
        String suffix = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(StringPool.DOT));
        String url = Objects.requireNonNull(OssFactory.build()).uploadSuffix(file.getBytes(), suffix);

        //保存文件信息
        File fileEntity = new File();


        Long newfolderId = IdWorker.getId();

        List<File> insertFile = new ArrayList<>();

        //如果有folder id  默认修改 上一次folderid
        if(!ObjectUtil.isNull(folderId)){
            //找到所有原有文件id
            List<File> list = fileService.list(Wrappers.lambdaQuery(File.class).eq(File::getFolderId, folderId));

            for (File newFile : list) {
                newFile.setId(null);
                newFile.setFolderId(newfolderId);
            }

            insertFile.addAll(list);

        }

        fileEntity.setFolderId(newfolderId);
        fileEntity.setFileUrl(url);
        fileEntity.setFileType(suffix);
        fileEntity.setFileName(file.getOriginalFilename());
        fileEntity.setFileSize(file.getSize());
        if(StrUtil.isNotBlank(processId)) {
            fileEntity.setProcessId(processId);
        }

        insertFile.add(fileEntity);
        fileService.saveBatch(insertFile);

        return R.ok(BeanUtil.toBean(fileEntity, FileVo.class));
    }

    /**
     * 上传文件
     */
    @PostMapping("/multi-upload")
    @GlcsLog(value = "上传文件")
    @SneakyThrows
    public R multiUpload(@RequestParam("file") MultipartFile[] file, @RequestParam(value = "folderId",required = false) Long folderId,
                    @RequestParam(value = "processId",required = false) String processId) {
        if (ObjectUtil.isNull(file)) {
            throw new MyException("上传文件不能为空");
        }

        Long newfolderId = IdWorker.getId();

        List<File> insertFile = new ArrayList<>();

        //如果有folder id  默认修改 上一次folderid
        if(!ObjectUtil.isNull(folderId)){
            //找到所有原有文件id
            List<File> list = fileService.list(Wrappers.lambdaQuery(File.class).eq(File::getFolderId, folderId));

            for (File newFile : list) {
                newFile.setId(null);
                newFile.setFolderId(newfolderId);
            }

            insertFile.addAll(list);

        }

        //上传文件
        List<FileVo> resultList = new ArrayList<>();
        for (MultipartFile f : file) {
            String suffix = Objects.requireNonNull(f.getOriginalFilename()).substring(f.getOriginalFilename().lastIndexOf(StringPool.DOT));
            String url = Objects.requireNonNull(OssFactory.build()).uploadSuffix(f.getBytes(), suffix);

            //保存文件信息
            Long id = IdWorker.getId();
            File fileEntity = new File();
            fileEntity.setId(id);
            fileEntity.setFolderId(newfolderId);
            fileEntity.setFileUrl(url);
            fileEntity.setFileType(suffix);
            fileEntity.setFileName(f.getOriginalFilename());
            fileEntity.setFileSize(f.getSize());
            if(StrUtil.isNotBlank(processId)) {
                fileEntity.setProcessId(processId);
            }

            insertFile.add(fileEntity);
            resultList.add(BeanUtil.toBean(fileEntity, FileVo.class));
        }

        fileService.saveBatch(insertFile);

        return R.ok(resultList);
    }
}
